package cn.qqtheme.framework.picker;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import cn.qqtheme.framework.adapter.FileAdapter;
import cn.qqtheme.framework.adapter.PathAdapter;
import cn.qqtheme.framework.entity.FileItem;
import cn.qqtheme.framework.popup.ConfirmPopup;
import cn.qqtheme.framework.util.ConvertUtils;
import cn.qqtheme.framework.util.LogUtils;
import cn.qqtheme.framework.util.StorageUtils;
import cn.qqtheme.framework.widget.HorizontalListView;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.Locale;
import net.lingala.zip4j.util.InternalZipConstants;

/* loaded from: classes.dex */
public class FilePicker extends ConfirmPopup<LinearLayout> implements AdapterView.OnItemClickListener {
    public static final int DIRECTORY = 0;
    public static final int FILE = 1;
    private FileAdapter adapter;
    private CharSequence emptyHint;
    private TextView emptyView;
    private String initPath;
    private int mode;
    private OnFilePickListener onFilePickListener;
    private PathAdapter pathAdapter;

    @Retention(RetentionPolicy.SOURCE)
    /* loaded from: classes.dex */
    public @interface Mode {
    }

    /* loaded from: classes.dex */
    public interface OnFilePickListener {
        void onFilePicked(String str);
    }

    public FilePicker(Activity activity, int i) {
        super(activity);
        this.adapter = new FileAdapter();
        this.pathAdapter = new PathAdapter();
        this.emptyHint = Locale.getDefault().getDisplayLanguage().contains("中文") ? "<空>" : "<Empty>";
        setHalfScreen(true);
        try {
            this.initPath = StorageUtils.getDownloadPath();
        } catch (RuntimeException unused) {
            this.initPath = StorageUtils.getInternalRootPath(activity);
        }
        this.mode = i;
        this.adapter.setOnlyListDir(i == 0);
        this.adapter.setShowHideDir(false);
        this.adapter.setShowHomeDir(false);
        this.adapter.setShowUpDir(false);
    }

    /* JADX INFO: Access modifiers changed from: protected */
    @Override // cn.qqtheme.framework.popup.ConfirmPopup
    public LinearLayout makeCenterView() {
        LinearLayout linearLayout = new LinearLayout(this.activity);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(-1, -1));
        linearLayout.setOrientation(1);
        linearLayout.setBackgroundColor(-1);
        ListView listView = new ListView(this.activity);
        listView.setBackgroundColor(-1);
        listView.setDivider(new ColorDrawable(-2236963));
        listView.setDividerHeight(1);
        listView.setCacheColorHint(0);
        listView.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        listView.setAdapter((ListAdapter) this.adapter);
        listView.setOnItemClickListener(this);
        linearLayout.addView(listView);
        this.emptyView = new TextView(this.activity);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(-1, -1);
        layoutParams.gravity = 17;
        this.emptyView.setLayoutParams(layoutParams);
        this.emptyView.setGravity(17);
        this.emptyView.setVisibility(8);
        this.emptyView.setTextColor(-16777216);
        linearLayout.addView(this.emptyView);
        return linearLayout;
    }

    @Override // cn.qqtheme.framework.popup.ConfirmPopup
    protected View makeFooterView() {
        LinearLayout linearLayout = new LinearLayout(this.activity);
        linearLayout.setLayoutParams(new LinearLayout.LayoutParams(-1, -2));
        linearLayout.setOrientation(1);
        linearLayout.setBackgroundColor(-1);
        View view = new View(this.activity);
        view.setLayoutParams(new LinearLayout.LayoutParams(-1, 1));
        view.setBackgroundColor(-2236963);
        linearLayout.addView(view);
        HorizontalListView horizontalListView = new HorizontalListView(this.activity);
        horizontalListView.setLayoutParams(new LinearLayout.LayoutParams(-1, ConvertUtils.toPx(this.activity, 30.0f)));
        horizontalListView.setAdapter((ListAdapter) this.pathAdapter);
        horizontalListView.setBackgroundColor(-1);
        horizontalListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { // from class: cn.qqtheme.framework.picker.FilePicker.1
            @Override // android.widget.AdapterView.OnItemClickListener
            public void onItemClick(AdapterView<?> adapterView, View view2, int i, long j) {
                FilePicker filePicker = FilePicker.this;
                filePicker.refreshCurrentDirPath(filePicker.pathAdapter.getItem(i));
            }
        });
        linearLayout.addView(horizontalListView);
        return linearLayout;
    }

    public void setRootPath(String str) {
        this.initPath = str;
    }

    public void setAllowExtensions(String[] strArr) {
        this.adapter.setAllowExtensions(strArr);
    }

    public void setShowUpDir(boolean z) {
        this.adapter.setShowUpDir(z);
    }

    public void setShowHomeDir(boolean z) {
        this.adapter.setShowHomeDir(z);
    }

    public void setShowHideDir(boolean z) {
        this.adapter.setShowHideDir(z);
    }

    public void setFileIcon(Drawable drawable) {
        this.adapter.setFileIcon(drawable);
    }

    public void setFolderIcon(Drawable drawable) {
        this.adapter.setFolderIcon(drawable);
    }

    public void setHomeIcon(Drawable drawable) {
        this.adapter.setHomeIcon(drawable);
    }

    public void setUpIcon(Drawable drawable) {
        this.adapter.setUpIcon(drawable);
    }

    public void setArrowIcon(Drawable drawable) {
        this.pathAdapter.setArrowIcon(drawable);
    }

    public void setItemHeight(int i) {
        this.adapter.setItemHeight(i);
    }

    public void setEmptyHint(CharSequence charSequence) {
        this.emptyHint = charSequence;
    }

    @Override // cn.qqtheme.framework.popup.BasicPopup
    protected void setContentViewBefore() {
        boolean z = this.mode == 1;
        setCancelVisible(!z);
        if (z) {
            setSubmitText(this.activity.getString(17039360));
        } else {
            setSubmitText(this.activity.getString(17039370));
        }
    }

    @Override // cn.qqtheme.framework.popup.BasicPopup
    protected void setContentViewAfter(View view) {
        refreshCurrentDirPath(this.initPath);
    }

    @Override // cn.qqtheme.framework.popup.ConfirmPopup
    protected void onSubmit() {
        if (this.mode == 1) {
            LogUtils.verbose("pick file canceled");
            return;
        }
        String currentPath = this.adapter.getCurrentPath();
        LogUtils.debug("picked directory: " + currentPath);
        OnFilePickListener onFilePickListener = this.onFilePickListener;
        if (onFilePickListener != null) {
            onFilePickListener.onFilePicked(currentPath);
        }
    }

    @Override // cn.qqtheme.framework.popup.BasicPopup
    public void dismiss() {
        super.dismiss();
    }

    public FileAdapter getAdapter() {
        return this.adapter;
    }

    public PathAdapter getPathAdapter() {
        return this.pathAdapter;
    }

    public String getCurrentPath() {
        return this.adapter.getCurrentPath();
    }

    @Override // android.widget.AdapterView.OnItemClickListener
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
        FileItem item = this.adapter.getItem(i);
        if (item.isDirectory()) {
            refreshCurrentDirPath(item.getPath());
            return;
        }
        String path = item.getPath();
        if (this.mode == 0) {
            LogUtils.warn("not directory: " + path);
            return;
        }
        dismiss();
        LogUtils.debug("picked path: " + path);
        OnFilePickListener onFilePickListener = this.onFilePickListener;
        if (onFilePickListener != null) {
            onFilePickListener.onFilePicked(path);
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void refreshCurrentDirPath(String str) {
        if (str.equals(InternalZipConstants.ZIP_FILE_SEPARATOR)) {
            this.pathAdapter.updatePath(InternalZipConstants.ZIP_FILE_SEPARATOR);
        } else {
            this.pathAdapter.updatePath(str);
        }
        this.adapter.loadData(str);
        int count = this.adapter.getCount();
        if (this.adapter.isShowHomeDir()) {
            count--;
        }
        if (this.adapter.isShowUpDir()) {
            count--;
        }
        if (count < 1) {
            LogUtils.verbose(this, "no files, or dir is empty");
            this.emptyView.setVisibility(0);
            this.emptyView.setText(this.emptyHint);
            return;
        }
        LogUtils.verbose(this, "files or dirs count: " + count);
        this.emptyView.setVisibility(8);
    }

    public void setOnFilePickListener(OnFilePickListener onFilePickListener) {
        this.onFilePickListener = onFilePickListener;
    }
}
